<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index:1201;">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="6">
          <el-form-item label="组织机构" prop="busMainData.otherSysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.otherSysOrgCode" :tax-rate="form.busMainData.taxRate" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="true" />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              size="mini"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户" prop="busMainData.sysOwnerCpy">
            <CpyOrgSetSelectDialog
              v-model="form.busMainData.otherCpyName"
              :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'"
              :cpy-code="form.busMainData.sysOwnerCpy"
              :ori-org-code="form.busMainData.sysOrgCode"
              cpy-owner-type="custom"
              @cpySelectCallback="cpySelectCallback"
              @orgSetSelectCallback="orgSetSelectCallback" />
          </el-form-item>
          <el-form-item label="发票类型" prop="busMainData.invoiceType">
            <el-select v-model="form.busMainData.invoiceType" :style="style" size="mini" filterable placeholder="计价币别">
              <el-option
                v-for="(item) in dicts.bus_invoice_type"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
          </el-form-item>
          <el-form-item label="税率" prop="busMainData.taxRate">
            <el-input-number v-model.number="form.busMainData.taxRate" :style="style" :controls="false" :precision="2" :min="0" :max="1" size="mini" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="本位币" prop="busMainData.priceCur">
            <el-select v-model="form.busMainData.priceCur" :disabled="true" :style="style" size="mini" filterable placeholder="计价币别">
              <el-option
                v-for="(item) in dicts.bus_price_cur"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
          </el-form-item>
          <el-form-item prop="busMainData.oriPriceCur">
            <template slot="label">
              原币/汇率<IconHelp display-content="汇率=本位币/原币.如本位币为人民币，原币为美元: 汇率为:0.1439." />
            </template>
            <el-select v-model="form.busMainData.oriPriceCur" style="width:100px;" size="mini" filterable placeholder="计价币别">
              <el-option
                v-for="(item) in dicts.bus_price_cur"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
            <el-input-number v-model.number="form.busMainData.excRate" :controls="false" :precision="6" :min="0" :max="1000" size="mini" style="width:80px;" />
          </el-form-item>
          <el-form-item label="附档" prop="attachmentList">
            <FileUpload
              ref="fileUpload"
              :editable="form.busPageInfo.editable"
              :sup_this="sup_this"
              :file-limit="5"
              :is-direct-upload="true"
              list-type="list"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="采购产品" name="1">
        <po-detail ref="poDetail" :po-details="form.poDetails" :editable="form.busPageInfo.editable" @setDetails="setDetails" @refreshPaymetDetail="refreshPaymetDetail"/>
      </el-collapse-item>
      <el-collapse-item title="付款方式" name="2">
        <po-payment-detail ref="poPaymentDetail" :po-payment-details="form.poPaymentDetails" :editable="form.busPageInfo.editable" :total-money="totalMoney" @setDetails="setPaymentDetails"/>
      </el-collapse-item>
      <el-collapse-item title="收货信息" name="3">
        <po-ext ref="poExt" :po-ext="form.poExt" :editable="form.busPageInfo.editable" />
      </el-collapse-item>
    </el-collapse>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_PO_ALL','PURCHASE_PO_CHANGE','PURCHASE_PO_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_PO_ALL','PURCHASE_PO_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import po from '@/api/business/Po'
import PoPaymentDetail from './poPaymentDetail'
import PoDetail from './poDetail'
import PoExt from './poExt'
import CpyOrgSetSelectDialog from '@/views/components/CpyOrgSetSelectDialog'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'

export default {
  components: { PoExt, PoDetail, PoPaymentDetail, CpyOrgSetSelectDialog, OrgsetTreeSelect },
  dictTypes: ['bus_price_cur', 'bus_invoice_type'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      isRedirect: false,
      loading: false,
      style: 'width: 184px',
      activeNames: ['1', '2', '3'],
      dialogInfo: {
        name: 'poOtherMngFormDialog',
        title: '客户采购单',
        fullScreen: true,
        width: 500,
        height: 90
      },
      form: {
        poDetails: [],
        poPaymentDetails: [],
        poExt: {},
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.busDate': [
          { required: true, message: '请选择业务时间', trigger: 'blur' }
        ],
        'busMainData.sysOrgCode': [
          { required: true, message: '请选择机构', trigger: 'blur' }
        ],
        'busMainData.otherSysOwnerCpy': [
          { required: true, message: '请选择供应商', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    totalMoney: function() {
      return this.getDetailTotalMoney()
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      this.isRedirect = false
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        po.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.poDetails = res.data
        })
        po.getPaymentDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.poPaymentDetails = res.data
        })
      }
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200)
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      po.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(id, false, false)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    cpySelectCallback(selectedRow, selectObj) {
      this.form.busMainData.otherSysOwnerCpy = ''
      this.form.busMainData.busOtherCpyName = ''
      if (selectedRow) {
        this.form.busMainData.otherSysOwnerCpy = selectedRow.cpyCode
        this.form.busMainData.busOtherCpyName = selectedRow.cpyLongName
      }
      selectObj.cancel()
    },
    orgSetSelectCallback(orgCode) {
      this.form.busMainData.otherSysOrgCode = orgCode
    },
    setDetails(details) {
      this.form.poDetails = details
    },
    getDetailTotalMoney() {
      let totalMoney = 0
      this.form.poDetails.forEach(tmprow => {
        totalMoney = totalMoney + tmprow.money
      })
      return totalMoney
    },
    refreshPaymetDetail() {
      const totalMoney = this.getDetailTotalMoney()
      const refPaymentDetail = this.$refs.poPaymentDetail
      this.form.poPaymentDetails.forEach(tmprow => {
        refPaymentDetail.calPaymetDetail(totalMoney, tmprow)
      })
    },
    setPaymentDetails(details) {
      this.form.poPaymentDetails = details
    }
  }
}
</script>
